class Solution {
public:
    int numberOfAlternatingGroups(vector<int>& colors, int k) {
        int n = colors.size(), ans = 0;
        int left = 0, right = 1;
        while (right < n + k - 1) {
            while (right < n + k - 1&& colors[right % n] != colors[(right - 1) % n]) {
                right++;
            }
            // cout << left <<' ' << right <<endl;
            ans += max(0, right - k - left + 1);
            left = right;
            right++;
        }
        return ans;
    }
};